<?php
	class CriarPesquisaTabela
	{
		public static function execute($objTabela)
		{
			for($i = 0; $i < count($objTabela->getCampos()); $i++)
		    {
		    	if($objTabela->getKey($i) == 'PRI')
		    	{
		    		$where .= ($i == 0) ? "'" . $objTabela->getCampo($i) . "=' . " . '$' . $objTabela->getCampo($i) . '[$i]' : " . '" . '&' . "' . '" . $objTabela->getCampo($i) . "=' . " . '$' . $objTabela->getCampo($i) . '[$i]';
		    	}
		    }

		    $conteudo .= "\t" . 'include_once(' . "'" . '../config/config.inc' . "');\n";
		    $conteudo .= "\t" . 'include_once($raiz . ' . "'" . '/connections/banco.inc' . "');\n";

		    $conteudo .= "\n\t" . '$sql  = ' . "'" . 'SELECT * FROM ' . $objTabela->getNomeTabela() . ' WHERE ' . "'" . ' . $_POST[' . "'" . 'campo' . "'" . ']' . " . '" . ' LIKE ' . "'" . ' . "' . "'" . '%' . '" . ' . '$_POST[' . "'" . 'valor' . "'" . ']' . " . " . '"' . '%' . "'" . '" . ' . "'" . ' ORDER BY ' . "'" . ' . $_POST[' . "'" . 'campo' . "'" . '];';
		    $conteudo .= "\n\t" . '$sql .= ' . '!$_POST[' . "'" . 'ordem' . "'" . '] ? ' . "'" . ';' . "'" . ' : ' . "'" . ' DESC;' . "';" . "\n";
		    $conteudo .= "\n\t" . '$_SESSION[' . "'" . 'sql' . "'" . '] = $sql;' . "\n\n";
		    $conteudo .= "\t" . '$cons = ' . $GLOBALS['banco'] . '_query($sql);' . "\n";
		    $conteudo .= "\t" . 'while($linha = ' . $GLOBALS['banco'] . '_fetch_array($cons))' . "\n\t{\n";

		    for($i = 0; $i < count($objTabela->getCampos()); $i++)
		    {
		    	$conteudo .= (strstr($objTabela->getCampo($i), 'data') == '') || strtolower($objTabela->getCampo($i)) == 'data_db' ? ("\t\t" . '$' . $objTabela->getCampo($i) . '[] = $linha[' . "'" . $objTabela->getCampo($i) . "'" . '];' . "\n") : ("\t\t" . '$' . $objTabela->getCampo($i) . '[] = (strlen($linha[' . "'" . $objTabela->getCampo($i) . "'" . ']) > 0) ? Util::dataBancoExibir($linha[' . "'" . $objTabela->getCampo($i) . "'" . '])' . ' : $linha[' . "'" . $objTabela->getCampo($i) . "'" . '];');
		    }

		    $conteudo .= "\t\t" . '$x++;' . "\n";
		    $conteudo .= "\t}\n?>\n";

		    $conteudo .= '<div align="left"><a href="../xls/xls.php"><img src="../imagens/ico_excel.gif" border="0"> Exportar</a></div>' . "\n" . '<table width="100%" border="1" cellspacing="0" cellpadding="0">' . "\n";
		    $conteudo .= "\t" . '<tr bgColor="#EEEEEE">' . "\n";

		    for($i = 0; $i < count($objTabela->getCampos()); $i++)
		    {
		    	$conteudo .= "\t\t" . '<td><strong><?= $matTabelasColunas[' . "'" . $objTabela->getNomeTabela() . "'" . '][' . "'" . $objTabela->getCampo($i) . "'" . '][' . "'" . 'nome' . "'" . '] ?></strong></td>' . "\n";
		    }

		    $conteudo .= "\t" . '</tr>' . "\n";
		    
		    $conteudo .= "<?\n\t" . '$bgColor = true;' . "\n\t" . 'for($i = 0; $i < $x; $i++)' . "\n\t{\n\t\t" . '$bgColor = !$bgColor;' . "\n?>\n";
		    for($i = 0; $i < count($objTabela->getCampos()); $i++)
		    {
		    	$conteudo .= $i == 0 ? "\t" . '<tr style="cursor:hand;" <?= !$bgColor ? ' . "'" . 'bgColor="#FFFFFF"' . "'" . ' : ' . "'" . 'bgColor="#EEEEEE"' . "'" . ' ?> onClick="JavaScript:location = ' . "'" . '../tl/tl_consulta_' . strtolower($objTabela->getNomeTabela()) . '.php?<?= ' . $where . ' ?>' . "'" . '" onMouseOver="bgColor=' . "'" . '#FFF79A' . "'" . ';" onMouseOut="bgColor=' . "'" . '<?= !$bgColor ? ' . "'" . '#FFFFFF' . "'" . ' : ' . "'" . '#EEEEEE' . "'" . ' ?>' . "'" . ';">' . "\n" : '';
		    	$conteudo .= "\t\t" . '<td><?= strlen($' . $objTabela->getCampo($i) . '[$i]) > 0 ? $' . $objTabela->getCampo($i) . '[$i] : ' . "'" . '&nbsp;' . "'" . ' ?></td>' . "\n";
		    	$conteudo .= $i == count($objTabela->getCampos()) - 1 ? "\t" . '</tr>' . "\n" : '';
		    }
		    
		    $conteudo .= '<?' . "\n\t" . '}' . "\n" . '?>' . "\n" . '</table>';
		    
		    return $conteudo;
		}
	}
?>